package com.lkwlw.tplms.transport.action;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.lkwlw.tplms.transport.dao.mapper.SchedulingResourcesMapper;
import com.lkwlw.tplms.transport.dao.mapper.VehicleinfoMapper;
import com.lkwlw.tplms.transport.dao.model.SchedulingResources;
import com.lkwlw.tplms.transport.dao.model.Vehicleinfo;
import com.lkwlw.tplms.transport.dao.model.VehicleinfoExample;
import com.lkwlw.tplms.transport.dao.model.VehicleinfoExample.Criteria;
import com.opensymphony.xwork2.ActionSupport;

public class SchedulingResourcesAction extends ActionSupport {
	private static final long serialVersionUID = -616570944138614329L;
	
	private VehicleinfoMapper vehicleinfoMapper;

	private SchedulingResourcesMapper schedulingResourcesMapper;
	
	private List<Vehicleinfo> vehicleinfo;
	
	private String id;
	
	private SchedulingResources sched;
	
	private String startAddres;
	
	private String passToAddres;
	
	private String destination;
	
	private String st;
	
	private Integer schedId;
	
	private List<Map<String,Object>> sches; 
	
	public String execute(){
		VehicleinfoExample example = new VehicleinfoExample(); 
		vehicleinfo = vehicleinfoMapper.selectByExample(example);
		
		sches = schedulingResourcesMapper.selectSchedulingResources(new HashMap());
		
		return SUCCESS;
	}

	public String gotoHaulway(){
//		sche.setStartTo(startTo);
		
		
//		schedulingResourcesMapper
		
		return SUCCESS;
	}
	
	public String addPassTo(){
		try {
			if(startAddres!=null){
			startAddres = new String(startAddres.getBytes("ISO-8859-1"),"utf-8");
			}
			if(passToAddres!=null){
				passToAddres = new String(passToAddres.getBytes("ISO-8859-1"),"utf-8");
			}
			if(destination!=null){
				destination = new String(destination.getBytes("ISO-8859-1"),"utf-8");
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		SchedulingResources sche = new SchedulingResources();
		sche.setDestination(destination);
		sche.setStartTo(startAddres);
		sche.setPassTo(passToAddres);
		sche.setVehicleId(id);
		sche.setStatus(1);
		schedulingResourcesMapper.insertSelective(sche);
		
		VehicleinfoExample example = new VehicleinfoExample();
		Criteria c = example.createCriteria();
		c.andVehiclenoEqualTo(String.valueOf(id));
		Vehicleinfo record = new Vehicleinfo();
		record.setState("出车");
		vehicleinfoMapper.updateByExampleSelective(record, example);
		return SUCCESS;
	}
	
	/**
	 * 修改车辆的状态
	 * @return
	 */
	public String modifyVehicleState(){
		if(st!=null){
			try {
				st=new String(st.getBytes("ISO-8859-1"),"utf-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
		Vehicleinfo record = new Vehicleinfo();
		record.setState(st);
		record.setVehicleno(String.valueOf(id));
		vehicleinfoMapper.updateByPrimaryKeySelective(record);
		if(schedId!=null){
			SchedulingResources sche = new SchedulingResources();
			sche.setId(schedId);
			sche.setStatus(2);
			schedulingResourcesMapper.updateByPrimaryKeySelective(sche);
		}
		return SUCCESS;
	}
	public void setVehicleinfoMapper(VehicleinfoMapper vehicleinfoMapper) {
		this.vehicleinfoMapper = vehicleinfoMapper;
	}

	public List<Vehicleinfo> getVehicleinfo() {
		return vehicleinfo;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getId() {
		return id;
	}

	public void setSchedulingResourcesMapper(SchedulingResourcesMapper schedulingResourcesMapper) {
		this.schedulingResourcesMapper = schedulingResourcesMapper;
	}

	public void setStartAddres(String startAddres) {
		this.startAddres = startAddres;
	}

	public void setPassToAddres(String passToAddres) {
		this.passToAddres = passToAddres;
	}

	public void setDestination(String destination) {
		this.destination = destination;
	}

	public SchedulingResources getSched() {
		return sched;
	}

	public List<Map<String, Object>> getSches() {
		return sches;
	}

	public void setSt(String st) {
		this.st = st;
	}

	public void setSchedId(Integer schedId) {
		this.schedId = schedId;
	}
    
}
